package com.wisely.highlight_spring4.ch3.taskexecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName TaskExecutorConfig
 * @Package com.wisely.highlight_spring4.ch3.taskexecutor
 * @Description TODO
 * @Author zjb
 * @Date 2018/7/20 18:04
 * @Version 1.0
 **/
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch3.taskexecutor")
//利用@EnableAsync注解开启异步任务支持
@EnableAsync
//配置类实现AsyncConfigurer接口
public class TaskExecutorConfig implements AsyncConfigurer {
    @Override
    //重写getAsyncExecutor方法，并返回一个ThreaPoolTaskExecutor,这样我们就获得了一个基于线程池TaskExecutor
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(25);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
